package com.yld.car.market;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import cn.yld.car.adapter.CarTypeAdapter;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.Car;
import com.yld.car.domain.SearchDetailCar;

public class CarKeyWordActivity extends BaseActivity implements
		OnItemClickListener {

	private EditText searchKeyEdit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.car_key_word);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_car_search);

		showButton();
		this.searchKeyEdit = ((EditText) findViewById(R.id.searchKey));
		// this.searchKeyEdit.addTextChangedListener(this.textWatcher);
		this.searchKeyEdit.setHint("可输入车型、颜色、配置...");

		utils = NetworkProgressUtils.getInstance();

		listView = (ListView) findViewById(R.id.list1);
		listView.setOnItemClickListener(this);

		mBar = (ProgressBar) findViewById(R.id.progressBar);
	}

	private ProgressBar mBar;
	private ListView listView = null;

	private void showButton() {

		Button localButton = (Button) findViewById(R.id.leftButton);
		localButton.setVisibility(View.VISIBLE);

		localButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();

			}
		});
	}

	public void onAction(View v) {
		// System.out.println("onAction===" +
		// searchKeyEdit.getText().toString());
		new KeyWordTask().execute("");

	}

	private NetworkProgressUtils utils;

	private class KeyWordTask extends AsyncTask<String, String, ArrayList<Car>> {

		@Override
		protected ArrayList<Car> doInBackground(String... params) {

			HashMap<String, String> allParams = new HashMap<String, String>();
			allParams.put("keyWords", searchKeyEdit.getText().toString());
			allParams.put("color", "");
			allParams.put("type", "");
			allParams.put("port", "");
			allParams.put("flag", "2");
			allParams.put("nowPage", "1");
			allParams.put("pageSize", "20");
			Object o = utils.getWebservicesByData(
					ConstantUtils.NAME_SPACE,
					ConstantUtils.ALL_METHROD_NAME.get(6),
					ConstantUtils.NAME_SPACE
							+ ConstantUtils.ALL_METHROD_NAME.get(6),
					ConstantUtils.GET_PAGE_DATA_CARS_URL, allParams);

			System.out.println("carKeyWord====" + o);
			// System.out.println("=======================");

			ArrayList<Car> all = utils.parseJson(o.toString(), "table",
					NetworkProgressUtils.SEARCH_CAR_TYPE,true);
			return all;
		}

		@Override
		protected void onPostExecute(ArrayList<Car> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			mBar.setVisibility(View.GONE);
			if (result != null && result.size() > 0) {

				CarTypeAdapter adapter = new CarTypeAdapter(result,
						CarKeyWordActivity.this);
				listView.setVisibility(View.VISIBLE);
				listView.setAdapter(adapter);
			}

		}

		@Override
		protected void onPreExecute() {

			mBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {

		Object o = adapterView.getAdapter().getItem(position);
		if (o instanceof SearchDetailCar) {
			final SearchDetailCar s = (SearchDetailCar) o;
			String cId = s.getId();
			Intent intent = new Intent();
			intent.putExtra("cId", cId);
			intent.setClass(this, CarDetailInfoActivity.class);
			startActivity(intent);
		}

	}
}
